const fs = require('fs');
const path = require('path');

// 创建dist目录
const distDir = path.join(__dirname, 'dist');
if (!fs.existsSync(distDir)) {
  fs.mkdirSync(distDir, { recursive: true });
  console.log('Created dist directory');
}

// 复制index.html
const indexHtml = path.join(__dirname, 'index.html');
const distIndexHtml = path.join(distDir, 'index.html');
fs.copyFileSync(indexHtml, distIndexHtml);
console.log('Copied index.html to dist');

// 复制static目录
const staticDir = path.join(__dirname, 'static');
const distStaticDir = path.join(distDir, 'static');

function copyDirectory(src, dest) {
  if (!fs.existsSync(dest)) {
    fs.mkdirSync(dest, { recursive: true });
  }
  
  const files = fs.readdirSync(src);
  for (const file of files) {
    const srcPath = path.join(src, file);
    const destPath = path.join(dest, file);
    
    const stats = fs.statSync(srcPath);
    if (stats.isDirectory()) {
      copyDirectory(srcPath, destPath);
    } else {
      fs.copyFileSync(srcPath, destPath);
    }
  }
}

copyDirectory(staticDir, distStaticDir);
console.log('Copied static directory to dist');

// 复制其他必要文件
const copyFiles = ['favicon.ico'];
copyFiles.forEach(file => {
  const source = path.join(__dirname, file);
  const destination = path.join(distDir, file);
  if (fs.existsSync(source)) {
    fs.copyFileSync(source, destination);
    console.log(`Copied ${file} to dist`);
  }
});

console.log('Build completed successfully!');